package com.coderman.topn;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Random;

/**
 * @Author fanchunshuai
 * @Date 2019/11/22 22
 * @Description:
 */
public class DataFile {
    public static void main(String[] args) {
        String filePath = "D:\\tmp\\topn\\topnall\\allnumberfiles\\data_all_topnumbers.txt";
        generateAllRandomNumberData(filePath, 300000000);
    }

    public static void generateSplitRandomNumberData(String filePath) {
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            String newfilePath = filePath + "\\data_" + i + ".txt";
            for (int j = 0; j < 30000; j++) {
                StringBuilder builder = new StringBuilder();
                for (int k = 0; k < 100000; k++) {
                    int x = random.nextInt(1000000);
                    if (x % 3 == 0 || x % 2 == 0 || x % 5 == 0) {
                        builder.append(x + ",");
                    }
                    String randomStr = random.nextInt(100000) + ",";
                    builder.append(randomStr);
                }
                builder.append(random.nextInt(10000000) + "\n");
                appendData(filePath, builder.toString());
            }
        }
    }

    /**
     * 生成一个大文件
     *
     * @param filePath
     * @param randomNumber
     */
    public static void generateAllRandomNumberData(String filePath, long randomNumber) {
        long startTime = System.currentTimeMillis();
        for (int k = 0; k < 10; k++) {
            Random random = new Random();
            StringBuilder builder = new StringBuilder();
            for (long j = 0; j < randomNumber; j++) {
                if (j % 2 == 0 || j % 3 == 0) {
                    int num = random.nextInt(100000000) % 3;
                    builder.append(num + ",");
                } else if (j % 5 == 0) {
                    builder.append(j + ",");
                }
                if (j % 10000 == 0) {
                    builder.append(random.nextInt(10000) + "\n");
                    appendData(filePath, builder.toString());
                    builder = new StringBuilder();
                }
            }
        }
        long endTime = System.currentTimeMillis();
        long seconds = (endTime - startTime) / 1000;
        System.out.println("usetime = " + seconds + "s");
    }


    private static void appendData(String filePath, String conent) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true)));
            out.write(conent);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 生成随机电话号码
     * @param filePath
     * @param randomNumber
     */
    public static void generateAllRandomHandPhoneData(String filePath, long randomNumber) {
        long startTime = System.currentTimeMillis();
        for (int k = 0; k < 10; k++) {
            Random random = new Random();
            StringBuilder builder = new StringBuilder();
            for (long j = 0; j < randomNumber; j++) {
                if (j % 2 == 0 || j % 3 == 0) {
                    String handPhone = getRandomHandPhone("139");
                    builder.append(handPhone + ",");
                } else if (j % 5 == 0) {
                    String handPhone = getRandomHandPhone("135");
                    builder.append(handPhone + ",");
                }
                if (j % 10000 == 0) {
                    String handPhone = getRandomHandPhone("138");
                    builder.append(handPhone + ",");
                    appendData(filePath, builder.toString());
                    builder = new StringBuilder();
                }
            }
            if(k % 2 == 0){
                for (long j = 0; j < randomNumber; j++) {
                    if (j % 2 == 0 || j % 3 == 0) {
                        String handPhone = getRandomHandPhone("188");
                        builder.append(handPhone + ",");
                    } else if (j % 5 == 0) {
                        String handPhone = getRandomHandPhone("136");
                        builder.append(handPhone + ",");
                    }
                    if (j % 10000 == 0) {
                        String handPhone = getRandomHandPhone("157");
                        builder.append(handPhone + ",");
                        appendData(filePath, builder.toString());
                        builder = new StringBuilder();
                    }
                }
            }
        }
        long endTime = System.currentTimeMillis();
        long seconds = (endTime - startTime) / 1000;
        System.out.println("usetime = " + seconds + "s");
    }

    /**
     * 生成单个随机电话号码
     * @param headPhone
     * @return
     */
    private static String getRandomHandPhone(String headPhone) {
        //9代表循环九次，产生九个随机号码
        //定义电话号码以139开头
        String number = headPhone;
        //定义random，产生随机数
        Random random = new Random();
        for (int j = 0; j < 8; j++) {
            //生成0~9 随机数
            number += random.nextInt(9);
        }
        //输出一个电话号码
        return number;
    }
}
